package org.albertsanso.yoigo.shopchat;

import java.util.Timer;
import java.util.TimerTask;

import org.albertsanso.yoigo.shopchat.co.ShopChatComponent;
import org.albertsanso.yoigo.shopchat.model.Mensaje;
import org.albertsanso.yoigo.shopchat.model.Tienda;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;

public class MyService extends Service {

	private static final String TAG = "MyService";

	private static Timer timer = new Timer();
	private Context ctx;
	
	final Handler messageHandler = new Handler();
	final Runnable showMessage = new Runnable() {
		public void run() {
			//Toast.makeText(ctx, "Fin Hay mensaje", Toast.LENGTH_SHORT).show();
		}
	};

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		//Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
		Log.d(TAG, "onCreate");

		this.ctx = this;
		startService();
	}

	private void startService() {
		timer.scheduleAtFixedRate(new mainTask(), 5000, 5000);
	}

	private class mainTask extends TimerTask {
		public void run() {
			
			YoigoShopChatActivity mainActivity = YoigoShopChatContext.getMainActivity();
			ShopChatComponent shopChatComponent = YoigoShopChatContext.getShopChatComponent();
			
			if (mainActivity != null) {
				if (shopChatComponent != null) {
					
					Tienda tienda = mainActivity.getTienda();
					if (tienda != null) {
						String codigoTiendaId = tienda.getCodigo();
						String numeroTelefono =  shopChatComponent.getMyPhoneNumber();
						Boolean hayMensaje = shopChatComponent.hayMensajeByTiendaIdAndNumeroTelefono(codigoTiendaId, numeroTelefono);
						Mensaje mensaje = shopChatComponent.getUltimoMensaje(codigoTiendaId, numeroTelefono);
						
						if (hayMensaje) {
							
							String ns = Context.NOTIFICATION_SERVICE;
							NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
							
							int icon = R.drawable.mensaje;
							CharSequence tickerText = mensaje.getQuien() + " : " + mensaje .getTexto();
							long when = System.currentTimeMillis();
			
							Notification notification = new Notification(icon, tickerText, when);
							notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;
							
							Context context = getApplicationContext();
							CharSequence contentTitle = mensaje.getQuien() + " : " + mensaje .getTexto();
							CharSequence contentText = mensaje.getQuien() + " : " + mensaje .getTexto();
							Intent notificationIntent = new Intent(ctx, YoigoShopChatActivity.class);
							PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, notificationIntent, 0);
			
							notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
							
							mNotificationManager.notify(1, notification);
						}
					}
				}
			}
		}
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		//Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
		Log.d(TAG, "onDestroy");
	}

	@Override
	public void onStart(Intent intent, int startid) {
		//Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
		Log.d(TAG, "onStart");
	}
}